package com.kcnet.qk.netty;

import android.util.Log;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 *
 * Created by LiuSaibao on 11/23/2016.
 */
public class NettyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private NettyListener listener;
    public NettyClientHandler(NettyListener listener){
        this.listener = listener;
    }
    
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		Log.d("html","channelActive");
		NettyClient.getInstance().setConnectStatus(true);
		if(listener!=null)listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_SUCCESS);
	}
	
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		Log.d("html","channelInactive");
		NettyClient.getInstance().setConnectStatus(false);
		if(listener!=null)listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_CLOSED);
		NettyClient.getInstance().reconnect();
	}

	@Override
	protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
		if(listener!=null)listener.onMessageResponse(byteBuf);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		Log.d("HTML","exceptionCaught："+cause.getMessage());
		ctx.close();
		NettyClient.getInstance().setConnectStatus(false);
		if(listener!=null)listener.onServiceStatusConnectChanged(NettyListener.STATUS_CONNECT_CLOSED);
		NettyClient.getInstance().reconnect();
	}

	/*@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

		if (evt instanceof IdleStateEvent) {
			IdleStateEvent event = (IdleStateEvent) evt;
			if (event.state() == IdleState.READER_IDLE){
				ctx.close();
			}else if (event.state() == IdleState.WRITER_IDLE){
				try{
					ctx.channel().writeAndFlush("Chilent-Ping\r\n");
				} catch (Exception e){
					Timber.e(e.getMessage());
				}
			}
		}
		super.userEventTriggered(ctx, evt);
	}*/
}
